package com.light.wanting.emoji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.light.applet.common.config.CommonQiniuConfig;
import com.light.applet.common.utils.UserUtils;
import com.light.wanting.emoji.*;
import com.light.wanting.emoji.mapper.ContentMapper;
import com.light.wanting.emoji.service.*;
import com.light.wanting.emoji.store.ContentFileStoreHolder;
import com.light.wanting.emoji.vo.ContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 魏彪
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private LikedService likedService;

    @Autowired
    private CollectedService collectedService;

    @Autowired
    private ContentFileStoreHolder contentFileStoreHolder;

    @Autowired
    private ContentFileService contentFileService;

    @Override
    public Content findById(Long id) {
        Content content = getById(id);
        // 查询关联关系
        LambdaQueryWrapper<ContentFile> contentFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        contentFileLambdaQueryWrapper.eq(ContentFile::getContentId, id);
        List<ContentFile> list = contentFileService.list(contentFileLambdaQueryWrapper);
        List<Long> fileIds = list.stream().map(ContentFile::getFileId).collect(Collectors.toList());
        // 查询图片
        LambdaQueryWrapper<File> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileLambdaQueryWrapper.in(File::getId, fileIds);
        List<File> files = fileService.list(fileLambdaQueryWrapper);
        List<File> source = files.stream().filter(e -> e.getUrl().contains("source")).collect(Collectors.toList());
        List<File> compress = files.stream().filter(e -> !e.getUrl().contains("source")).collect(Collectors.toList());

        for (File sourceFile : source) {
            CommonQiniuConfig holderBucket = contentFileStoreHolder.findBucket(sourceFile.getBucketName());
            sourceFile.setSourceUrl(fileService.generateLink(sourceFile.getUrl(), holderBucket));
            for (File compressFile : compress) {
                String sourceFlag = sourceFile.getUrl().split("\\.")[0];
                String compressFlag = compressFile.getUrl().split("\\.")[0];
                if (sourceFlag.contains(compressFlag)) {
                    sourceFile.setUrl(fileService.generateLink(compressFile.getUrl(), holderBucket));
                }
            }
        }

        content.setImages(source);
        return content;
    }

    @Override
    public ContentVo like(Long id) {
//        ContentVo contentVo = findById(id);
//
//        if (contentVo.getLiked()) {
//            LambdaQueryWrapper<Liked> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Liked::getContentId, id)
//                    .eq(Liked::getUserId, UserUtils.currentUser().getId());
//            likedService.remove(queryWrapper);
//
//            contentVo.setLiked(false);
//            contentVo.setLikedNum(contentVo.getLikedNum() - 1);
//
//            Content content = new Content();
//            content.setLikedNum(contentVo.getLikedNum());
//            content.setId(id);
//            saveOrUpdate(content);
//
//            return contentVo;
//        }
//
//        Liked newLiked = new Liked();
//        newLiked.setContentId(id);
//        newLiked.setUserId(UserUtils.currentUser().getId());
//        newLiked.setCreateTime(new Date());
//        likedService.saveOrUpdate(newLiked);
//
//        contentVo.setLiked(true);
//        contentVo.setLikedNum(contentVo.getLikedNum() + 1);
//
//        Content content = new Content();
//        content.setLikedNum(contentVo.getLikedNum());
//        content.setId(id);
//        saveOrUpdate(content);

        return null;
    }

    private Liked getLiked(Long id) {
        LambdaQueryWrapper<Liked> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Liked::getContentId, id).eq(Liked::getUserId, UserUtils.currentUser().getId());
        return likedService.getOne(queryWrapper);
    }

    @Override
    public ContentVo collect(Long id) {
//        ContentVo contentVo = findById(id);
//
//        if (contentVo.getCollected()) {
//            LambdaQueryWrapper<Collected> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Collected::getContentId, id)
//                    .eq(Collected::getUserId, UserUtils.currentUser().getId());
//            collectedService.remove(queryWrapper);
//
//            contentVo.setCollected(false);
//            contentVo.setCollectedNum(contentVo.getCollectedNum() - 1);
//
//            Content content = new Content();
//            content.setCollectedNum(contentVo.getCollectedNum());
//            content.setId(id);
//            saveOrUpdate(content);
//
//            return contentVo;
//        }
//
//        Collected newCollected = new Collected();
//        newCollected.setContentId(id);
//        newCollected.setUserId(UserUtils.currentUser().getId());
//        newCollected.setCreateTime(new Date());
//        collectedService.saveOrUpdate(newCollected);
//
//        contentVo.setCollectedNum(contentVo.getCollectedNum() + 1);
//        contentVo.setCollected(true);
//
//        Content content = new Content();
//        content.setId(id);
//        content.setCollectedNum(contentVo.getCollectedNum());
//        saveOrUpdate(content);
        return null;
    }

    @Override
    public IPage<ContentVo> liked(IPage<Liked> page) {
//        LambdaQueryWrapper<Liked> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Liked::getUserId, UserUtils.currentUser().getId());
//        IPage<Liked> likedIPage = likedService.page(page, queryWrapper);
//        if (ObjectUtils.isEmpty(likedIPage)) {
//            return null;
//        }
//
//        IPage<ContentVo> contentVoIPage = new Page<>();
//        contentVoIPage.setTotal(likedIPage.getTotal());
//        contentVoIPage.setPages(likedIPage.getPages());
//        contentVoIPage.setCurrent(likedIPage.getCurrent());
//        contentVoIPage.setSize(likedIPage.getSize());
//
//        List<ContentVo> contentVoList = new ArrayList<>();
//        for (Liked record : likedIPage.getRecords()) {
//            ContentVo vo = findById(record.getContentId());
//            contentVoList.add(vo);
//        }
//
//        contentVoIPage.setRecords(contentVoList);

        return null;
    }

    @Override
    public IPage<ContentVo> collected(IPage<Collected> page) {
//        LambdaQueryWrapper<Collected> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Collected::getUserId, UserUtils.currentUser().getId());
//        IPage<Collected> collectedIPage = collectedService.page(page, queryWrapper);
//        if (ObjectUtils.isEmpty(collectedIPage)) {
//            return null;
//        }
//
//        IPage<ContentVo> contentVoIPage = new Page<>();
//        contentVoIPage.setTotal(collectedIPage.getTotal());
//        contentVoIPage.setPages(collectedIPage.getPages());
//        contentVoIPage.setCurrent(collectedIPage.getCurrent());
//        contentVoIPage.setSize(collectedIPage.getSize());
//
//        List<ContentVo> contentVoList = new ArrayList<>();
//        for (Collected record : collectedIPage.getRecords()) {
//            ContentVo vo = findById(record.getContentId());
//            contentVoList.add(vo);
//        }
//
//        contentVoIPage.setRecords(contentVoList);

        return null;
    }

    @Override
    public IPage<Content> findPage(Page<Content> page, Content content) {
        IPage<Content> contentIPage = contentMapper.findPage(page, content);
        for (Content record : contentIPage.getRecords()) {
            CommonQiniuConfig holderBucket = contentFileStoreHolder.findBucket(record.getCoveredImg().getBucketName());
            record.getCoveredImg().setUrl(fileService.generateLink(record.getCoveredImg().getUrl(), holderBucket));
        }
        return contentIPage;
    }

    private Collected getCollected(Long id) {
        LambdaQueryWrapper<Collected> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collected::getContentId, id).eq(Collected::getUserId, UserUtils.currentUser().getId());
        return collectedService.getOne(queryWrapper);
    }
}
